package com.taotao.druid.sqlparser.like;

import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 获取like 里的参数在参数列表位置
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2023/6/14 13:46
 */
public class LikeParamIndexesVisitor extends MySqlASTVisitorAdapter {

    private List<Integer> params;
    private List<Integer> indexes = new ArrayList<>();

    public LikeParamIndexesVisitor() {

    }

    public LikeParamIndexesVisitor(List<Integer> params) {
        this.params = params;
    }


    public boolean visit(SQLVariantRefExpr x) {
        System.out.println(" @@@@@@@@@@ " + x + "    " + x.getIndex() + "  " + params.get(x.getIndex()));


        return true;
    }


    @Override
    public boolean visit(SQLBinaryOpExpr x) {

        if ("like".equalsIgnoreCase(x.getOperator().getName())) {
            System.out.println(" ***** like start " + x);
        }


        return true;
    }


    @Override
    public void endVisit(SQLBinaryOpExpr x) {
        if ("like".equalsIgnoreCase(x.getOperator().getName())) {
            System.out.println(" ***** like end " + x);
            System.out.println();
        }
    }



    @Override
    public boolean visit(SQLMethodInvokeExpr x) {
//
//        if ("concat".equalsIgnoreCase(x.getMethodName())) {
//            System.out.println("########## " + x.getArguments());
//        }


        return true;
    }
}
